from tortoise.models import Model
from tortoise import fields
from datetime import datetime


class Warehouse(Model):
    """
    仓库信息模型
    """
    whid = fields.IntField(pk=True, description="仓库ID")
    whname = fields.CharField(max_length=20, null=True, description="名称")
    whcode = fields.CharField(max_length=20, null=True, description="编码")
    whtype = fields.CharField(max_length=20, null=True, description="类型")
    tag = fields.CharField(max_length=20, null=True, description="标签")
    statu = fields.CharField(max_length=20, null=True, description="状态")
    orderid = fields.IntField(null=True, description="排序")
    remark = fields.CharField(max_length=255, null=True, description="备注")
    del_flag = fields.CharField(max_length=1, default="0", description="删除标志(0代表存在 2代表删除)")
    create_by = fields.CharField(max_length=64, null=True, description="创建者")
    create_time = fields.DatetimeField(null=True, description="创建时间")
    update_by = fields.CharField(max_length=64, null=True, description="更新者")
    update_time = fields.DatetimeField(null=True, description="更新时间")
    userid = fields.IntField(null=True, description="业务员")

    class Meta:
        table = "wms_warehouse"
        table_description = "仓库信息表"

    def __str__(self):
        return f"Warehouse(whid={self.whid}, whname={self.whname})"

    @property
    def is_active(self) -> bool:
        """判断仓库是否激活"""
        return self.statu == "0"

    @property
    def is_deleted(self) -> bool:
        """判断仓库是否被删除"""
        return self.del_flag == "2"

    @classmethod
    async def get_active_warehouses(cls):
        """获取所有激活的仓库"""
        return await cls.filter(statu="0", del_flag__not="2")

    @classmethod
    async def get_by_whname(cls, whname: str):
        """根据仓库名称获取仓库"""
        return await cls.filter(whname=whname, del_flag__not="2").first()